home *** CD-ROM | disk | FTP | other *** search
/ PC-Blue - MS DOS Public Domain Library / PC-Blue MS-DOS Public Domain Library - NYACC.iso / vol086 / roff4.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-12-15  |  13.3 KB  |  525 lines

  1. /********************************************************/
  2. /*                            */
  3. /*            ROFF4, Version 1.60            */
  4. /*                            */
  5. /*(C) 1983,4 by Ernest E. Bergmann            */
  6. /*        Physics, Building #16            */
  7. /*        Lehigh Univerisity            */
  8. /*        Bethlehem, Pa. 18015            */
  9. /*                            */
  10. /* Permission is hereby granted for all commercial and    */
  11. /* non-commercial reproduction and distribution of this    */
  12. /* material provided this notice is included.        */
  13. /*                            */
  14. /********************************************************/
  15. /********************************************************/
  16. /* March 1984  DeSmet C version */
  17. /* Henry Harpending*/
  18. /* Anthropology Department*/
  19. /* University of New Mexico*/
  20. /* Albuquerque, NM  87131*/
  21. /*   no restrictions */
  22. /*********************************************************/
  23. #include "a:roff4.h"
  24. /*****************MAIN************MAIN*********/
  25. main(argc, argv)
  26. int argc;
  27. char **argv;
  28. {char option,*macq(),*pc;
  29. char filename[20];
  30. struct divfd *pd;
  31. int i;
  32.  
  33. fprintf(stderr,"ROFF4: MS-DOS version 1.61 of August 1984\n");
  34. fprintf(stderr,"            (c) 1983,4 by\n");
  35. fprintf(stderr,"   E. E. Bergmann, 730 Seneca Street\n");
  36. fprintf(stderr,"           Bethlehem, PA 18015\n\n\n");
  37.  
  38. ofp=stdout;
  39. SUPPRESS= debug = FALSE;
  40.  
  41. if (argc == 1)
  42.       { fprintf(stderr,"USAGE:  ROFF4 file1\n  more than one file OK\n");
  43.     exit();
  44.       }
  45.  
  46. for(i=1;i<argc;i++){
  47.     strcpy (filename,argv[i]);
  48.      if(filename[0]=='}'){
  49.          printf("Opening output file <%s>\n",&filename[1]);
  50.         if(( ofp = fopen(&filename[1],"w")) == ERROR){
  51.             printf("Error on opening <%s>\n",&filename[1]);
  52.                exit();
  53.           }
  54.     break;
  55.      }
  56. }
  57.  
  58. argv++;
  59. init_defaults();
  60. while ( --argc > 0 )
  61.       { strcpy (filename, *argv++);
  62.     if(*filename=='>')goto SKIPIT;
  63.     if(*filename=='}')goto SKIPIT;
  64.     fprintf(stderr,"Now processing <%s>\n",filename );
  65.     if(filename[0]=='-')
  66.         {option=toupper(filename[1]);
  67.         if(option=='M') showm();
  68.         else if(option=='B') debug=TRUE;
  69.         else if(option=='D') showd();
  70.         else if(option=='F') putc(FORMF,ofp);
  71.         else if(option=='G') gloss();
  72.         else if(option=='I') showit();
  73.         else if(option=='R') showr();
  74.         else if(option=='S') PAGESTOP=TRUE;
  75.         else if(option=='O') range(&filename[2]);
  76.         else    {KEYBD=option;
  77.             dolns();
  78.             fprintf(stderr,"End of keyboard input <%c>\n",
  79.                 KEYBD);
  80.             KEYBD=FALSE;
  81.             }
  82.         SKIPIT: continue;
  83.         }
  84.     if(pd=find2(filename,DLINK)) dclose(pd);
  85.     if (ERROR == (ifp=fopen( filename,"r")))
  86.         {fprintf(stderr,"can't open <%s>\n",filename );
  87.         continue;
  88.         }
  89.     else dolns();
  90.     if(VLINENO>0||OUTBUF[0]) space(HUGE);
  91.     fprintf(stderr,"Done processing <%s>\n", filename );
  92.     close(ifp);
  93. }
  94. dsclose();
  95. close(ofp);
  96. }/* end main()           */
  97.  
  98. /****************************************/
  99. dolns()    /*do processing of lines*/
  100. {char *pc;
  101. int typ;
  102. int e;
  103.   BINP=0;
  104.    while((e=fgets2(LINE,ifp))||FPTR) /*until EOF or CPMEOF */
  105.       {if(e) {if(LINE[0]==COMMAND)
  106.         {if(pc=macq(LINE)) pbstr(pc);
  107.         else comand(LINE);
  108.         }
  109.           else text(LINE);
  110.          }
  111.     else endso();
  112.       }
  113. }
  114. /**************************************************************
  115. initializes the global variables governing the execution of the
  116.  format commands.
  117. **************************************************************/
  118. init_defaults()
  119. {initsk(&FILL,FI_DEF);    /* yes we want filled lines */
  120. initsk(&LSVAL,LS_DEF);    /* line spacing = 1 */
  121. initsk(&INVAL,IN_DEF);    /* left margin indent  0 */
  122. initsk(&RMVAL,RM_DEF);    /* right margin = page width  */
  123. TIVAL = TI_DEF;    /* left margin temporary indent    0 */
  124. CEVAL = 0;    /* next n lines to be centered -  0  */
  125. PAGESTOP = FALSE;
  126. FFEED = FF_INI;
  127. FIRSTPAGE=1;
  128. LASTPAGE=30000;/*infinite*/
  129. JUSTIFY=JU_INI;
  130. initsk(&PLVAL,PL_DEF);
  131. initsk(&M1VAL,M1_DEF);
  132. initsk(&M2VAL,M2_DEF);
  133. initsk(&M3VAL,M3_DEF);
  134. initsk(&M4VAL,M4_DEF);
  135. initsk(&SCVAL,SC_INI);
  136. initsk(&OWVAL,OW_INI);
  137. initsk(&TABSIZ,TS_DEF);
  138. initsk(&TCVAL,TC_DEF);
  139. initsk(&CFVAL,CF_DEF);
  140. initsk(&ICVAL,IC_DEF);
  141. CURPAG = 0;
  142. NEWPAG = 1;
  143. FRQ=FRSTRING=WHSTRING=0;
  144. FRVAL=1;
  145. FVLINENO=FPLINENO = PLINENO = 0;VLINENO = -1;
  146. BOTTOM = PLVAL - M3VAL - M4VAL;
  147. OUTW=OUTPOS=OUTTOP=OUTBOT=OLDLN=OLDBOT=OUTWRDS = 0;
  148. OUTBUF [0] = '\0';
  149. DIR = 0;
  150. EH2 = EH3 = EHEAD ;
  151. OH2 = OH3 = OHEAD ;
  152. *EHEAD = *OHEAD = '\0' ;
  153. EF2 = EF3 = EFOOT ;
  154. OF2 = OF3 = OFOOT ;
  155. *EFOOT = *OFOOT = '\0' ;
  156. _setmem(CPTR,2*(128-' '),0);
  157. _setmem(TPTR,2*(128-' '),0);
  158. TREND = TRTBL;
  159. OUTBUF2[0]=BPOS=0;
  160. initxu();
  161. MCNT=1;
  162. UF=XF=FALSE;
  163. _setmem(&DBUF,LSZ,FALSE);
  164. DPOS=-1;
  165. FPTR=DLINK=RLINK=SLINK=MLINK=0;
  166. KPTR=KLINE;
  167. *KLINE=0;
  168. KEYBD=FALSE;
  169.  
  170. complete();
  171. }
  172. /****************************************/
  173.  
  174. /*    July 1: \#\ replaced by current page number.
  175.     June 8: concatenate lines with trailing '\'
  176.     June 1, 1983 modified for macro string processing.
  177.     fgets2:    (May 2, 1983 by EEB fix to reset parity)
  178.     This next function is like "gets", except that
  179.     a) the line is taken from a buffered input file instead
  180.     of from the console, and b) the newline is INCLUDED in
  181.     the string and followed by a null byte. 
  182.     
  183.     This one is a little tricky due to the CP/M convention
  184.     of having a carriage-return AND a linefeed character
  185.     at the end of every text line. In order to make text
  186.     easier to deal with from C programs, ngetc()
  187.     automatically strips off the CR from any CR-LF
  188.     combinations that come in from the file. Any CR
  189.     characters not immediately followed by LF are left
  190.     intact. The LF is included as part of the string, and
  191.     is followed by a null byte. (Note that LF equals
  192.     "newline".) There is no limit to how long a line
  193.     can be here; care should be taken to make sure the
  194.     string pointer passed to fgets points to an area
  195.     large enough to accept any possible line length
  196.     (a line must be terminated by a newline (LF, or '\n')
  197.     character before it is considered complete.)  The
  198.     ngetc() also resets the parity bit.
  199.  
  200.     The value NULL (defined to be 0 here) is returned
  201.     on EOF, whether it be a physical EOF (attempting to
  202.     read past last sector of the file) OR a logical EOF
  203.     (encountered a control-Z.) The 1.3 version didn't
  204.     recognize logical EOFs, because I did't realize how
  205.     SIMPLE it was to implement a buffered I/O "ungetc"
  206.     function.
  207. */
  208.  
  209. fgets2(s,iobuf)
  210. char *s;
  211. FILE iobuf;
  212. {
  213.     int count,c,i,*pw,pnum;
  214.     char *cptr,*pc;
  215.     char wbuf[LSZ],*fnd;
  216.     count = MAXLINE;
  217.     cptr = s;
  218.     if ( (c=ngetc(iobuf))==EOF){
  219.          if(DEBUG)puts("\nfgets2 returning the value NULL\n");
  220.         return NULL;
  221.     }
  222.     do {    if(c==ICVAL)/*need macro substitution*/
  223.           {for(i=0;ICVAL!=(wbuf[i]=ngetc(iobuf));i++)
  224.             {if(wbuf[i]=='\n')
  225.                 {if(i) putback('\n');
  226.                 break;
  227.                 }
  228.             }
  229.           if(i) 
  230.            {wbuf[i]='\0';
  231.             if((*wbuf==NUMSIGN)&&(i==1))
  232.             {if((VLINENO>=PLVAL)
  233.                 ||(VLINENO<0)) pnum=NEWPAG;
  234.             else pnum=CURPAG;
  235.             itoc(pnum,wbuf,10);
  236.             pbstr(wbuf);
  237.             }
  238.             else if(pw=find2(wbuf,RLINK))
  239.             {itoc(*pw,wbuf,10);
  240.             pbstr(wbuf);
  241.             }
  242.             else if(fnd=find2(wbuf,SLINK)) pbstr(fnd);
  243.             else{start();
  244.                 pc=wbuf;
  245.             pw=TREND;
  246.             *(pw++)=SLINK;
  247.             transfer(&pc,&pw,'\0');
  248.             fprintf(stderr,"%cPlease define <%s>:",
  249.                         BELL,wbuf);
  250.             gets(wbuf);
  251.             pc=wbuf;
  252.             transfer(&pc,&pw,'\0');
  253.             SLINK=TREND;
  254.             TREND=pw;
  255.             complete();
  256.             pbstr(wbuf);
  257.             }
  258.             continue;
  259.             }
  260.            else if(*wbuf!='\n') putback(ICVAL); 
  261.            c=ngetc(iobuf);
  262.           }
  263.         if ((*cptr++ = c) == '\n') break;
  264.  
  265.      } while (count--&&(c=ngetc(iobuf))!=EOF);
  266.  
  267.  
  268.     *cptr = '\0';
  269.     return s;
  270. }
  271. /**************************************************************
  272. performs the formatting command returned by comtyp -sets global
  273.   variables ( indenting, underlining, etc. )
  274. **************************************************************/
  275. comand ( line )
  276. char *line;
  277. {int c_type;    /* command type  */
  278. int arg_val;    /* argument value, if any */
  279. char arg_typ;    /* relative (+ or -) or absolute */
  280. char wbuf[20];
  281. int i;
  282. c_type = comtyp (line);
  283. if DEBUG fprintf(stderr,"COMAND %d,",c_type);
  284. if (c_type == UNKNOWN)
  285.     {fprintf(stderr, "UNKNOWN COMMAND: <%s>\n", line);
  286.     return;
  287.     }
  288. arg_val = get_val ( line, &arg_typ );
  289. if DEBUG
  290. fprintf(stderr,"get_val returned arg_val=%d,arg_typ=%c\n",
  291.                    arg_val,    arg_typ   );
  292. switch (c_type)
  293.     {case IG : break;/* ignore remark */
  294.  
  295.     case FI :    /* filled lines  */
  296.         brk();
  297.         FILL = YES;
  298.         break;
  299.  
  300.     case NF :    /* non-filled lines */
  301.         brk();
  302.         FILL = NO;
  303.         break;
  304.  
  305.     case NJ :    /* non-justified lines */
  306.         JUSTIFY = NO;
  307.         break;
  308.  
  309.     case JU :    /* justified lines  */
  310.         JUSTIFY = YES;
  311.         break;
  312.  
  313.     case BR :    /* just cause a break */
  314.         brk();
  315.         break;
  316.  
  317.     case LS :    /* set line spacing value */
  318.         setS(&LSVAL, arg_val, arg_typ, LS_DEF, 1, HUGE );
  319.         break;
  320.  
  321.     case TI :    /* set temporary left indent */
  322.         brk();
  323.         set ( &TIVAL, arg_val, arg_typ, TI_DEF, 0, RMVAL );
  324.         break;
  325.  
  326.     case IN :    /* set left indent */
  327.         setS( &INVAL, arg_val, arg_typ, IN_DEF, 0, RMVAL-1 );
  328.         TIVAL = INVAL;
  329.         break;
  330.  
  331.     case RM:    /* set right margin */
  332.         setS( &RMVAL, arg_val, arg_typ, RM_DEF, TIVAL+1, OWVAL );
  333.         break;
  334.     case M1:    /* set topmost margin */
  335.         setS( &M1VAL, arg_val, arg_typ, M1_DEF,0,HUGE);
  336.         break;
  337.  
  338.     case M2:    /* set second top margin */
  339.         setS( &M2VAL, arg_val, arg_typ, M2_DEF,0,HUGE);
  340.         break;
  341.  
  342.     case M3:    /* set first bottom margin */
  343.         setS( &M3VAL, arg_val, arg_typ, M3_DEF,0,HUGE);
  344.         BOTTOM = PLVAL - M3VAL - M4VAL;
  345.         break;
  346.  
  347.     case M4:    /* set bottom-most margin */
  348.         setS(&M4VAL, arg_val, arg_typ, M4_DEF,0,HUGE);
  349.         BOTTOM = PLVAL - M3VAL - M4VAL;
  350.         break;
  351.  
  352.     case CE :    /* center next arg_val lines */
  353.         brk();
  354.         set ( &CEVAL, arg_val, arg_typ,CE_DEF,0, HUGE);
  355.         break;
  356.  
  357.     case HE :    /* get header title for pages */
  358.         gettl3 ( line, EHEAD, &EH2, &EH3 );
  359.         gettl3 ( line, OHEAD, &OH2, &OH3 );
  360.         break;
  361.  
  362.     case OH :    /*get odd header title*/
  363.         gettl3 ( line, OHEAD, &OH2, &OH3 );
  364.         break;
  365.  
  366.     case EH :    /*get even header title*/
  367.         gettl3 ( line, EHEAD, &EH2, &EH3 );
  368.         break;
  369.  
  370.     case FO :    /* get footer title for pages */
  371.         gettl3 ( line, EFOOT, &EF2, &EF3 );
  372.         gettl3 ( line, OFOOT, &OF2, &OF3 );
  373.         break;
  374.  
  375.     case OF :    /* get odd page footer title*/
  376.         gettl3 ( line, OFOOT, &OF2, &OF3 );
  377.         break;
  378.  
  379.     case EF :    /* get even page footer title*/
  380.         gettl3 ( line, EFOOT, &EF2, &EF3 );
  381.         break;
  382.  
  383.     case SP :    /* space down arg_val blank lines */
  384.         set (&SPVAL, arg_val, arg_typ, 1, 0, HUGE);
  385.         space ( SPVAL );
  386.         break;
  387.  
  388.     case ST :    /* stop(pause) at each page?*/
  389.         set(&PAGESTOP,arg_val,'0',YES,NO,YES);
  390.         break;
  391.     case BP :    /* set pageno arg_val - begin page */
  392.         brk();
  393.         if(((VLINENO<=0)||(VLINENO>=BOTTOM))&&
  394.             (arg_val==NO_VAL)) break;
  395.         if ( VLINENO > 0 )    space (HUGE);
  396.         set(&CURPAG,arg_val,arg_typ,CURPAG+1,0,9999);
  397.         NEWPAG = CURPAG;
  398.         break;
  399.     case NE :    /*"need"*/
  400.         if (arg_val==NO_VAL) arg_val=2;/*default*/
  401.         need(arg_val);
  402.         break;
  403.  
  404.     case PL :    /* set page length */
  405.         setS(&PLVAL, arg_val, arg_typ, PL_DEF,
  406.           M1VAL+M2VAL+M3VAL+M4VAL+1,HUGE);
  407.         BOTTOM = PLVAL - M3VAL - M4VAL;
  408.         break;
  409.  
  410.     case FF :    /*formfeed*/
  411.         set(&FFEED,arg_val,'0',FF_DEF,NO,YES);
  412.         break;
  413.  
  414.     case SC :    /*space character*/
  415.         if(arg_typ) arg_val=arg_typ;
  416.         setS(&SCVAL,arg_val,'0',SC_INI,BLANK,127);
  417.         break;
  418.  
  419.     case OW :    /*output device width*/
  420.         setS(&OWVAL,arg_val,'0',OW_INI,RMVAL,HUGE);
  421.         break;
  422.     case TS :    /*tabsize*/
  423.         setS(&TABSIZ,arg_val,'0',TS_DEF,1,HUGE);
  424.         break;
  425.  
  426.     case AB :    /*abort*/
  427.         fprintf(stderr,"\n***USER ABORT***\n");
  428.         exit();
  429.  
  430.     case DB :    /*debug*/
  431.         setS(&debug,arg_val,'0',NO,NO,YES);
  432.         if DEBUG fprintf(stderr,"\nDEBUG ON...\n");
  433.         else fprintf(stderr,"\n...END OF DEBUG\n");
  434.         break;
  435.  
  436.     case TC :    /*translate character flag*/
  437.         if(arg_typ) arg_val=arg_typ;
  438.         setS(&TCVAL,arg_val,'0',TC_DEF,BLANK+1,127);
  439.         break;
  440.  
  441.     case CF :    /*translate character flag*/
  442.         if(arg_typ) arg_val=arg_typ;
  443.         setS(&CFVAL,arg_val,'0',CF_DEF,BLANK+1,127);
  444.         break;
  445.  
  446.     case IC :    /*insert character for macro replace*/
  447.         if(arg_typ) arg_val=arg_typ;
  448.         setS(&ICVAL,arg_val,'0',IC_DEF,BLANK+1,127);
  449.         break;
  450.  
  451.     case TR :    /*translation string defined here*/
  452.         gettr(); break;
  453.  
  454.     case OU :    /*output code string*/
  455.         ocode(); break;
  456.  
  457.     case FR :    /*define fractional spacing code*/
  458.         getfr(); break;
  459.  
  460.     case WH :    /*define whole line spacing code*/
  461.         getwh(); break;
  462.  
  463.     case DS :    /*define string*/
  464.         insert(); break;
  465.  
  466.     case DM :    /*define macro*/
  467.         minsert(); break;
  468.  
  469.     case RG :    /*register variable*/
  470.         dovar(); break;
  471.  
  472.     case DI :    /*diversion to file*/
  473.         dodiv(); break;
  474.  
  475.     case SO :    /*source from file*/
  476.         source(); break;
  477.  
  478.     case PC :    /*printer control*/
  479.         getpc(); break;
  480.  
  481.     case SA :    /*"say" to console*/
  482.         getwrd(LINE,wbuf);    /*skip command*/
  483.         skip_blanks(LINE);
  484.         trunc_bl(LINE);
  485.         fprintf(stderr,"<%s>\n",LINE);
  486.         break;
  487.  
  488.     case BJ :    /*break with right justification*/
  489.         if(FILL)/*not applicable otherwise*/
  490.         {spread(OUTBUF,
  491.             min(RMVAL-TIVAL,MAXLINE-1)-OUTW+1,
  492.             OUTWRDS);
  493.         brk();
  494.         }
  495.         break;
  496.       }
  497. }
  498.  
  499. start()/*to insure only one entry into TRTBL at a time*/
  500. {if(TFLAG)
  501.     {fprintf(stderr,"ILLEGAL double access of TRTBL\n");
  502.     fprintf(stderr,"Simultaneous creation of two definitions not possible");
  503.     exit();
  504.     }
  505. else    TFLAG=TRUE;
  506. }
  507.  
  508. complete()/*to insure only one entry into TRTBL at a time*/
  509. {TFLAG=FALSE;
  510. }
  511.  
  512. range(s)
  513. char *s;
  514. {int num;
  515. num=0;
  516. while(isdigit(*s)) num=num*10+(*(s++)-'0');
  517. if(num) FIRSTPAGE=num;
  518. if(*s=='-')
  519.     {s++; num=0;
  520.     while(isdigit(*s)) num=num*10+(*(s++)-'0');
  521.     if(num) LASTPAGE=num;
  522.     }
  523. else    LASTPAGE=FIRSTPAGE;
  524. }
  525.